<template>
  <div class="closing">
    <div>
      <el-button size="mini" @click="handleGetUntreated">{{ $t('btn.reload') }}</el-button>
      <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
    </div>
    <div class="header">
      <jc-form
        ref="createBomForm"
        :option-value="subDetails"
        :options="prodOptions"
      />
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
      >
        <template v-slot:billSlot="col">
          <el-link type="primary" @click="pushView(col.scope.row)">{{ col.scope.row.fbillNo }}</el-link>
        </template>
      </jc-table>
    </div>
  </div>
</template>

<script>
import {
  GeneralLedgerDetailList
} from '@/api/financial/subject'
import jcForm from '@/components/Form'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
export default {
  name: 'BalanceSheetDetail',
  components: {
    jcForm
  },
  data() {
    return {
      subDetails: {},
      period: '',
      loading: false,
      // 表头
      tableData: [], // 销售数据
      tableHeight: '70vh',
      tableHeader: [
        { label: this.$t('market.fdate'), prop: 'voucherdate', width: '130' },
        { label: this.$t('market.voucherNum'), type: 'bill', prop: 'fbillNo', width: '130' },
        { label: this.$t('market.vwordId'), prop: 'vWordName', width: '100' },
        { label: this.$t('market.vNumber'), prop: 'vNumber', width: '130' },
        { label: this.$t('market.digest'), prop: 'description', width: '180' },
        { label: this.$t('market.amountIn'), prop: 'amountIn', width: '120' },
        { label: this.$t('market.amountOut'), prop: 'amountOut', width: '120' },
        { label: this.$t('market.direction'), prop: 'ftobalance', width: '80' },
        { label: this.$t('market.money'), prop: 'balanceAmount', width: '120' }
      ],
      prodOptions: {
        subjectName: { label: this.$t('market.subjectName'), span: 8, disabled: true },
        fiscalYear: { label: this.$t('market.accountingyear'), span: 8, disabled: true },
        period: { label: this.$t('market.period'), span: 8, disabled: true }
      }
    }
  },
  created() {
    this.handleGetUntreated()
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 200px)' : '73vh'
  },
  methods: {
    // 获取科目余额列表数据
    async handleGetUntreated() {
      this.subDetails = this.$route.query
      this.loading = true
      const DATA = { ...this.subDetails }
      const RES = await GeneralLedgerDetailList(DATA)
      if (RES.code === 0) {
        this.loading = false
        this.tableData = RES.data.list
      } else {
        this.loading = false
        this.$confirm(RES.message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    },
    // 跳转凭证
    pushView(row) {
      this.$router.push({ path: `/certificateDetail/${row.voucherId}` })
    },
    // 搜索
    handleQuerySonClass() {
      this.currentPage = 1
      this.handleGetUntreated()
    },
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          const list = this.tableData
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '科目余额表明细')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.closing {
  @include listBom;

  .header{
    margin-top: 10px;
    .header-name{
      span {
        width: 100px;
        margin-right: 10px;
        text-align: right;
      }
    }
  }
}
</style>
